package com.fenghuajueli.libbasecoreui.mvp;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.fenghuajueli.libbasecoreui.mvp.interfaces.DialogButtonListener;
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils;

public abstract class OtherBaseFragment extends BaseFragment {
    private View contentView;
    private Context context;
    private boolean currentVisibleState = false;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity().getApplicationContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = LayoutInflater.from(getActivity()).inflate(getContentViewId(), container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        contentView = null;
    }

    public View getContentView() {
        return contentView;
    }

    public Context getApplicationContext() {
        return context;
    }

    public abstract void initView(Bundle savedInstanceState);

    public abstract int getContentViewId();

    public View findViewById(int id) {
        if (contentView != null)
            return contentView.findViewById(id);
        return null;
    }


    @Override
    public void onResume() {
        super.onResume();
        //新版本
        if (!isHidden() && !currentVisibleState && isResumed()) {
            currentVisibleState = true;
            dispatchUserVisibleHint(true);
        }
    }

    /**
     * 当前界面可见不可见
     *
     * @param isVisible
     */
    protected void dispatchUserVisibleHint(boolean isVisible) {
//        if (isVisible) {
//            LogUtils.e("当前内容可见");
//        } else {
//            LogUtils.e("当前内容不可见");
//        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (currentVisibleState && !isResumed()) {
            currentVisibleState = false;
            dispatchUserVisibleHint(false);
        }
    }

    public void showDefalutTipsDialog(String title, String content, String confirmName, String cancelName, final DialogButtonListener dialogButtonListener) {
        builder = new AlertDialog.Builder(getActivity())
                .setMessage(content)
                .setTitle(title)
                .setPositiveButton(confirmName, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogButtonListener.onConfirm();
                    }
                }).setNegativeButton(cancelName, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogButtonListener.onCancel();
                    }
                });
        alertDialog = builder.create();
        alertDialog.show();
    }


    /**
     * 关闭提示框
     */
    public void hideTipsDialog() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
        builder=null;
    }

    /**
     * 调整布局到状态栏之下
     * @param view
     */
    public void changeViewBelowStatusBar(View view){
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams instanceof RelativeLayout.LayoutParams) {
            ((RelativeLayout.LayoutParams)layoutParams).topMargin= MyStatusBarUtils.getStatusBarHeight(getContext());
        } else if (layoutParams instanceof LinearLayout.LayoutParams) {
            ((LinearLayout.LayoutParams)layoutParams).topMargin= MyStatusBarUtils.getStatusBarHeight(getContext());
        }
        view.setLayoutParams(layoutParams);
    }
}
